# Correlate each ST with serogroups.
# Correlate each CC with serogroups.

use Bio::Perl;
use Bio::Tools::Mlst;
use Data::Dumper;
use warnings;
use strict;

my $database='neisseria';
my $ST=1;

getProbableSerogroup($ST);

sub getProbableSerogroup{
  my($st)=@_;
  my(@isolate,$numIsolates,$j,$cc);
  my (@ST,$numSts);
  my (%st_serogroup,%cc_serogroup); # each element will be like "numW=>5" or "numA=>1"
  my $pubmlst=Bio::Tools::Mlst->new({database=>$database});
  # get all STs
  $numSts=$pubmlst->getProfileCount;
  # get all isolates for the ST
  @isolate=$pubmlst->getRelatedIsolateIdsByST({ST=>$st,match=>7});
  $numIsolates=@isolate;
  for($j=0;$j<$numIsolates;$j++){
    my(%isolate,$isolate,$serogroup,$cc);
    $isolate=$isolate[$j];
    # get the count of each serogroup for the isolates
    %isolate=$pubmlst->getIsolate({id=>$isolate});
    $serogroup=$isolate{serogroup};
    next if(!$serogroup); # some just aren't grouped
    $st_serogroup{$st}{$serogroup}++;
  }
  print "$st\t";
  my $st_serogroup=$st_serogroup{$st};
  while(my($serogroup,$count)=each(%$st_serogroup)){
  	print "$serogroup:$count\t";
  }
  print "\n";
}